<?php
/*
 * 个人资料编辑
 * @package
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 3ea194d5523a5a5029aeee7a52b0088fb69a5765 $
 */

defined('IN_IXDCW') or exit('Access Denied');
login();
// edit by mark 2013-10-21
if (Cookie::get('coshop_flow')) {
	$businessAuthDb = Db('businessAuth');
	$businessAuthDb -> verify($_userid);
	Cookie::remove('coshop_flow');
	Header::location(url('member/business'));
}
// end edit by mark
require DT_CORE.'/'.$module.'/common.inc.php';
require_once MD_ROOT.'/member.class.php';
require_once MD_ROOT.'/memberIndustry.class.php';
require_once DT_INC.'/post.func.php';

$do = new member();
$do->userid = $_userid;
$user = $do->get_one();

$CATEGORY = PCache::read('category-4.php');
$cartree = Ext::genTree($CATEGORY,'catid');


$tab = isset($tab) ? intval($tab) : 0;
if($post) {
	if($post['password'] && $user['password'] != md5(md5($post['oldpassword']))) Prompt::message($L['error_password']);
	if($post['payword'] && $user['payword'] != md5(md5($post['oldpayword']))) Prompt::message($L['error_payword']);
	$post['groupid'] = $user['groupid'];
	$post['email'] = $user['email'];
	$post['passport'] = $user['passport'];
	$post['domain'] = $user['domain'];
	$post['icp'] = $user['icp'];
	$post['skin'] = $user['skin'];
	$post['template'] = $user['template'];
	$post['edittime'] = $DT_TIME;
	$post['bank'] = $user['bank'];
	$post['account'] = $user['account'];
	$post['validated'] = $user['validated'];
	$post['validator'] = $user['validator'];
	$post['validtime'] = $user['validtime'];
	$post['vemail'] = $user['vemail'];
	$post['vmobile'] = $user['vmobile'];
	$post['vtruename'] = $user['vtruename'];
	$post['vbank'] = $user['vbank'];
	$post['vcompany'] = $user['vcompany'];
	$post['vtrade'] = $user['vtrade'];
	$post['trade'] = $user['trade'];
	$post['support'] = $user['support'];
	$post['inviter'] = $user['inviter'];
	if($post['vmobile']) $post['mobile'] = $user['mobile'];
	if($post['vtruename']) $post['truename'] = $user['truename'];

	$post['idepartment'] = $post['department'];
	$post['icareer'] = $post['career'];

	if ($post['icats']) {
		if (empty($post['icompany']) || empty($post['icareer']) || empty($post['idepartment'])) {
			Prompt::message('行业圈资料不能为空');
		}
	}

	if(isset($post['areaid'])){
		if($db->count($db->getPre().'city','areaid='.$post['areaid'])){
				$post['cityid'] = $post['areaid'];
		}else{
			$parents = explode(',',$AREA[$post['areaid']]['arrparentid']);
			if(count($parents) <=1 )Prompt::message('请选择所在地区');
			foreach($parents as $v){
				if($db->count($db->getPre().'city','areaid='.$v)){
					$post['cityid'] = $v;
				}
			}
		};
	}
	if($do->edit($post)) {
		if($user['edittime'] == 0 && $user['inviter'] && $MOD['credit_user']) {
			$inviter = $user['inviter'];
			$r = $db->getOne("SELECT itemid FROM {$db->getPre()}finance_credit WHERE note='$_username' AND username='$inviter'");
			if(!$r) {
				credit_add($inviter, $MOD['credit_user']);
				credit_record($inviter, $MOD['credit_user'], 'system', $L['edit_invite'], $_username);
			}
		}
		if($user['edittime'] == 0 && $MOD['credit_edit']) {
			credit_add($_username, $MOD['credit_edit']);
			credit_record($_username, $MOD['credit_edit'], 'system', $L['edit_profile'], $DT_IP);
		}
		$jumpUrl = Cookie::has('jumpUrl') ? Cookie::get('jumpUrl') : Env::referer();
		Cookie::remove('jumpUrl');
		Prompt::message('资料修改成功', url('member/index'));
	} else {
		Prompt::message($do->errmsg);
	}

} else {
	$COM_TYPE = explode('|', $MOD['com_type']);
	$COM_SIZE = explode('|', $MOD['com_size']);
	$COM_MODE = explode('|', $MOD['com_mode']);
	$MONEY_UNIT = explode('|', $MOD['money_unit']);
	$head_title = $L['edit_title'];
	extract($user);
	$content_table = content_table(4, $userid, is_file(DT_CACHE.'/4.part'), $db->getPre().'company_data');
	$d = $db->getOne("SELECT content FROM {$content_table} WHERE userid=$userid");
	$introduce = $d['content'];
	$cates = $catid ? explode(',', $catid) : array();
	$tab = isset($tab) ? intval($tab) : -1;
	if($tab==1){
		$tabStr = '我的资料';
	}elseif($tab==2){
		$tabStr = '企业资料';
	}elseif($tab==3){
		$tabStr = '企业库';
	}elseif($tab==4){
		$tabStr = '修改密码';
	}
	include template('edit', $module);
}